# -*- coding: utf-8 -*-
# @File     : api.py
# @Author   : bingjia
# @Time     : 2020/8/7 9:56
# @Desc     : 租赁api视图

from django.shortcuts import get_list_or_404, get_object_or_404

from ..base.api import viewsets
from ..base.api import response

from . import models
from . import validators
from . import serializers
from . import permissions


class LeaseViewSet(viewsets.ModelViewSet):
    """
    平台视图
    """
    def list(self, request, *args, **kwargs):
        """
        群查（平台租赁）
        """


class LeasePlatformViewSet(viewsets.ModelViewSet):
    """
    平台租赁视图
    """

    def list(self, request, *args, **kwargs):
        """
        群查（平台租赁）
        """
    def retrieve(self, request, *args, **kwargs):
        """
        单查（平台租赁）
        """


class LeasePlatformAdminViewSet(viewsets.ModelViewSet):
    """
    平台租赁视图
    """
    def update(self, request, *args, **kwargs):
        """
        修改（平台租赁）
        """
    def destroy(self, request, *args, **kwargs):
        """
        删除（平台租赁）
        """
    def partial_update(self, request, *args, **kwargs):
        """
        部分修改(平台租赁)
        """


class LeaseServiceViewSet(viewsets.ModelViewSet):
    """
    服务租赁视图
    """
    queryset = models.LeaseService.objects.all()
    "验证"
    validator_class = validators.LeaseServiceValidator
    "序列化"
    serializer_class = serializers.LeaseServiceSerializer
    "权限"
    permission_classes = (permissions.LeaseServicePermission,)

    def list(self, request, *args, **kwargs):
        """
        群查（服务租赁）
        """
        self.check_permissions(request, "list", None)
        lease_service_obj_list = self.get_queryset()
        page = self.paginate_queryset(lease_service_obj_list)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        else:
            serializer = self.get_serializer(lease_service_obj_list, many=True)

        return response.Ok(serializer.data)

    def retrieve(self, request, *args, **kwargs):
        """
        单查（服务租赁）
        """
        self.check_permissions(request, "list", None)
        lease_service_obj = get_object_or_404(self.get_queryset(), **kwargs)
        serializer = self.get_serializer(lease_service_obj)
        return response.Ok(serializer.data)


class LeaseServiceAdminViewSet(viewsets.ModelViewSet):
    """
    服务租赁视图
    """
    queryset = models.LeaseService.objects.all()
    "验证"
    validator_class = validators.LeaseServiceValidator
    "序列化"
    serializer_class = serializers.LeaseServiceSerializer
    "权限"
    permission_classes = (permissions.LeaseServicePermission,)

    def list(self, request, *args, **kwargs):
        """
        群查（服务租赁）
        """
        self.check_permissions(request, "list", None)
        lease_service_obj_list = self.get_queryset()
        page = self.paginate_queryset(lease_service_obj_list)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        else:
            serializer = self.get_serializer(lease_service_obj_list, many=True)

        return response.Ok(serializer.data)

    def retrieve(self, request, *args, **kwargs):
        """
        单查（服务租赁）
        """
        self.check_permissions(request, "list", None)
        lease_service_obj = get_object_or_404(self.get_queryset(), **kwargs)
        serializer = self.get_serializer(lease_service_obj)
        return response.Ok(serializer.data)

    def update(self, request, *args, **kwargs):
        """
        修改（服务租赁）
        """

    def destroy(self, request, *args, **kwargs):
        """
        删除（服务租赁）
        """
        self.check_permissions(request, "list", None)
        lease_service_obj = get_object_or_404(self.get_queryset(), **kwargs)
        lease_service_obj.delete()
        return response.NoContent()

    def partial_update(self, request, *args, **kwargs):
        """
        部分修改(服务租赁)
        """


class LeaseModelViewSet(viewsets.ModelViewSet):
    """
    模型租赁视图
    """
    queryset = models.LeaseModel.objects.all()
    "验证"
    validator_class = validators.LeaseModelValidator
    "序列化"
    serializer_class = serializers.LeaseModelSerializer
    "权限"
    permission_classes = (permissions.LeaseModelPermission,)

    def list(self, request, *args, **kwargs):
        """
        群查（模型租赁）
        """
        self.check_permissions(request, "list", None)
        lease_model_obj_list = self.get_queryset()
        page = self.paginate_queryset(lease_model_obj_list)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        else:
            serializer = self.get_serializer(lease_model_obj_list, many=True)

        return response.Ok(serializer.data)

    def retrieve(self, request, *args, **kwargs):
        """
        单查（模型租赁）
        """
        self.check_permissions(request, "list", None)
        lease_model_obj = get_object_or_404(self.get_queryset(), **kwargs)
        serializer = self.get_serializer(lease_model_obj)
        return response.Ok(serializer.data)


class LeaseModelAdminViewSet(viewsets.ModelViewSet):
    """
    模型租赁视图
    """
    queryset = models.LeaseModel.objects.all()
    "验证"
    validator_class = validators.LeaseModelValidator
    "序列化"
    serializer_class = serializers.LeaseModelSerializer
    "权限"
    permission_classes = (permissions.LeaseModelPermission,)

    def list(self, request, *args, **kwargs):
        """
        群查（模型租赁）
        """
        self.check_permissions(request, "list", None)
        lease_model_obj_list = self.get_queryset()
        page = self.paginate_queryset(lease_model_obj_list)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        else:
            serializer = self.get_serializer(lease_model_obj_list, many=True)

        return response.Ok(serializer.data)

    def retrieve(self, request, *args, **kwargs):
        """
        单查（模型租赁）
        """
        self.check_permissions(request, "list", None)
        lease_model_obj = get_object_or_404(self.get_queryset(), **kwargs)
        serializer = self.get_serializer(lease_model_obj)
        return response.Ok(serializer.data)

    def update(self, request, *args, **kwargs):
        """
        修改（模型租赁）
        """

    def destroy(self, request, *args, **kwargs):
        """
        删除（模型租赁）
        """
        self.check_permissions(request, "list", None)
        lease_model_obj = get_object_or_404(self.get_queryset(), **kwargs)
        lease_model_obj.delete()
        return response.NoContent()

    def partial_update(self, request, *args, **kwargs):
        """
        部分修改(模型租赁)
        """
